import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;

/*
 * Settings.java - This class connects to Music class to change sound on/off. 
 * 					This class connects to Mysql class o change BilCIS password 
 * 					or Gmail account and its password. This class also connects to 
 * 					Back-groundGUI to change the background Color.
 * 
 * @Author : BilCIS
 */

public class Settings extends JPanel
{	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4175399157733039179L;

	// Variables declaration
	private	JLabel 			sound;
	
	private	JPanel 			contentPane;
	private	JPanel 			soundPane;
	private	JPanel			 securityPane;
	private	JPanel 			graphicPane;
	
	private	ButtonGroup		group;
	private	JRadioButton	soundOn;
	private	JRadioButton	soundOff;
	
	private	JButton			soundTest;
	private	JButton			changeBilCISPassword;
	private	JButton			changeMail;
	private	JButton 		setBackground;
	
	private	boolean			control = false;
	
	private Employee emp;
	private Mysql mysql;
	private	Music mus;
	// End of variables declaration

	public Settings( Employee emp)
	{
		mysql = new Mysql(emp.getID());
		mus = new Music();
		this.emp = emp;
		create();
		this.setVisible(true);
		control = false;
	}
	
	private void create()
	{
		//
		//Create controls
		//
		soundPane = new JPanel(null);
		contentPane = new JPanel(null);
		securityPane = new JPanel(null);
		graphicPane = new JPanel(null);
		//theme = new JComboBox
		group = new ButtonGroup();
		sound = new JLabel("Sound:");
		soundTest = new JButton("Test");
		changeBilCISPassword = new JButton("Change BilCIS Password");
		changeMail = new JButton("Change Mail");
		setBackground = new JButton("Background");
		
		if ( emp.getSound() == true )
		{
			mus.mute(false);
			soundOn = new JRadioButton("On", true );
			soundOff = new JRadioButton("Off");
		}
		else
		{
			mus.mute(true);
			soundOn = new JRadioButton("On");
			soundOff = new JRadioButton("Off", true);
		}
		//	
		//Register action listeners
		//
		sound_Action soundGroup  = new sound_Action();
		soundOn.addActionListener(soundGroup);
		soundOff.addActionListener(soundGroup);
		soundTest.addActionListener( new soundTest_Action() );
		changeBilCISPassword.addActionListener( new changeBilCISPassword_Action() );
		changeMail.addActionListener( new changeMail_Action() );
		setBackground.addActionListener( new setBackground_Action() );
		//	
		// Mnemonics
		//
		soundTest.setMnemonic('t');
		setBackground.setMnemonic('b');
		changeMail.setMnemonic('m');
		changeBilCISPassword.setMnemonic('b');
		//
		//Add controls to pane
		//
		this.add(contentPane);
		contentPane.add(soundPane);
		contentPane.add(securityPane);
		contentPane.add(graphicPane);
		soundPane.add(soundOn);
		soundPane.add(soundOff);
		soundPane.add(sound);
		soundPane.add(soundTest);
		securityPane.add(changeBilCISPassword);
		securityPane.add(changeMail);
		graphicPane.add(setBackground);
		group.add(soundOn);
		group.add(soundOff);
		//	
		//Set bounds
		//	
		contentPane.setBounds(0,0,400,300);
		soundPane.setBounds(10,0,380,100);
		securityPane.setBounds(10,220,380,80);
		graphicPane.setBounds(10,110,380,90);
		changeBilCISPassword.setBounds(180,25,180,40);
		changeMail.setBounds(15,25,150,40);
		setBackground.setBounds(125,25,150,40);
		soundOn.setBounds(170,30,50,50);
		soundOff.setBounds(110,30,50,50);
		sound.setBounds(30,30,70,50);
		soundTest.setBounds(250,40,75,40);
		//
		// Settings Borders
		//	
		soundPane.setBorder(BorderFactory.createTitledBorder("Sound"));
		securityPane.setBorder(BorderFactory.createTitledBorder("Security"));
		graphicPane.setBorder(BorderFactory.createTitledBorder("Graphics"));
		
		//********************** Colors *********************//
		contentPane.setBackground(emp.getColor());
		soundPane.setBackground(emp.getColor());
		securityPane.setBackground(emp.getColor());
		graphicPane.setBackground(emp.getColor());
		soundOn.setBackground(emp.getColor());
		soundOff.setBackground(emp.getColor());
		//
		// Settings
		//	
		this.setSize(new Dimension(400, 300));
		this.setLayout(null);
	}
	
	// switches sound on-off
	public class sound_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			Object source = e.getSource();
		
			if ( source == soundOn )
			{
				mus.mute(false);
				try {
					emp.setSound(true);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			else if ( source == soundOff )
			{
				mus.mute(true); 
				try {
					emp.setSound(false);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}
	}
	
	public boolean getControl()
	{
		return control;
	}
	
	// opens backgroundGUI class
	public class setBackground_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			control = true;
			Music color = new Music( new File("color.wav").getAbsolutePath() );
			color.play();
			new BackgroundGUI(emp);
		}
	}
	
	// changes mail values with new ones
	public class changeMail_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			
			String mail = JOptionPane.showInputDialog(null, "Enter your e-mail");
			try {
				mysql.setGmailUsername(mail);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			
			JPanel panel = new JPanel();
			JLabel label = new JLabel("Enter your password:  ");
			JPasswordField pass = new JPasswordField(10);
			
			panel.add(label);
			panel.add(pass);
			
			int select = JOptionPane.showConfirmDialog(null, panel);
			
			if ( JOptionPane.OK_OPTION == select )
			{
				char[] password = pass.getPassword();
				String mailPassword = new String(password);
				
				try {
					mysql.setGmailPassword(mailPassword);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}

		}
	}
	
	// changes bilcis password with given ones
	public class changeBilCISPassword_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			JPanel panel = new JPanel();
			JLabel label = new JLabel("Enter a password:  ");
			JPasswordField pass = new JPasswordField(10);
			
			panel.add(label);
			panel.add(pass);
			
			int select = JOptionPane.showConfirmDialog(null, panel);
			
			if ( JOptionPane.OK_OPTION == select )
			{
				char[] password = pass.getPassword();
				String BILpassword = new String(password);
			
				try {
					emp.setPassword(BILpassword);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}
	}
	
	// to test sound
	public class soundTest_Action implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			Music soundTesting = new Music( new File("soundtesting.wav").getAbsolutePath() );
			soundTesting.play();
		}
	}

}